package com.zl.learning.io;

import java.io.IOException;
import java.nio.file.*;

/**
 * Created by zhaolei on 2017/7/4.
 */
public class FileMonitor implements Runnable {
    @Override
    public void run() {
        try {
            WatchService watchService = FileSystems.getDefault().newWatchService();
            final Path path = Paths.get("conf");
            path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
            while (true) {
                final WatchKey wk = watchService.take();
                for (WatchEvent<?> event : wk.pollEvents()) {
                    String changed = event.context().toString();
                    System.out.println(changed);
                }
                boolean flag = wk.reset();
                if (!flag) {
                    break;
                }
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Thread(new FileMonitor()).start();
    }
}
